package com.zivwu.library;

import android.Manifest;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.PersistableBundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zivwu.library.ui.photo.PhotoPickerActivity;
import com.zivwu.library.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class ListAllActivity extends ListActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
        super.onStart();
        ArrayAdapter<Class> adapter = new ClassListAdapter(this,
                android.R.layout.simple_expandable_list_item_1,getStringList());
        getListView().setAdapter(adapter);

        RxPermissions permissions = new RxPermissions(this);
        permissions.requestEach(Manifest.permission.CAMERA,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {

                        } else if (!permission.shouldShowRequestPermissionRationale) {
                            ToastUtil.showToast(getApplicationContext(),
                                    R.string.ziv_permission_error_photo_picker);
                        }
                    }
                });

    }

    private List<Class> getStringList() {
        List<Class> list =new ArrayList<>();
        list.add(PhotoPickerActivity.class);
        list.add(MainActivity.class);
        list.add(TestBannerActivity.class);
        return list;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Object item = l.getAdapter().getItem(position);
        if (item instanceof Class){
            Class c= (Class) item;
            startActivity(new Intent(this,c));
        }
        super.onListItemClick(l, v, position, id);
    }

    private class ClassListAdapter extends ArrayAdapter<Class>{

        private final int mResource;
        public ClassListAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<Class> objects) {
            super(context, resource, objects);
            mResource=resource;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
           View view = LayoutInflater.from(ListAllActivity.this)
                    .inflate(mResource, parent, false);
            TextView text = (TextView) view.findViewById(android.R.id.text1);
            Class item = getItem(position);
            String name = item.getName();
            String substring = name.substring(name.lastIndexOf(".")+1, name.length());
            text.setText(substring);
            return view;
        }
    }
}
